<?php

use yii\helpers\Html;
use yii\widgets\DetailView;
use backend\assets\PrintAsset;
PrintAsset::register($this);
$this->title = '结算单'.$model->number;

/* @var $this yii\web\View */
/* @var $model common\models\business\BusinessBackpigFinish */

$this->params['breadcrumbs'][] = ['label' => 'Business Backpig Finishes', 'url' => ['index']];
$this->params['breadcrumbs'][] = $this->title;


//购买总费用
$buytotal = 0;
//回购总费用
$backtotal = 0;
//总体养殖成本
$costtotal = 0;
?>


<section class="wrapper site-min-height">
    <!-- page start-->
    <section class="panel">
        <header class="panel-heading">
            <?= Html::a('返回','javascript:history.back(-1)'); ?>
            <?= Html::encode($this->title) ?>
        </header>
        <div class="panel-body">
            <div class="adv-table editable-table ">

                <div class="business-backpig-finish-view">
                    <p>
                        <?= Html::a('更新', ['update', 'id' => $model->id], ['class' => 'btn btn-primary']) ?>
                        <?= Html::a('删除', ['delete', 'id' => $model->id], [
                            'class' => 'btn btn-danger',
                            'data' => [
                                'confirm' => '您确定删除此项吗?',
                                'method' => 'post',
                            ],
                        ]) ?>
                    </p>

                    <?= DetailView::widget([
                        'model' => $model,
                        'attributes' => [
                            'id',
                            'number',
                            'totalbackprice',
                            'finishprice',
                            'remark',
                            [
                                'attribute' => 'createtime',
                                'value' => date('Y-m-d',$model->createtime)
                            ],
                            [
                                'attribute' => 'status',
                                'value' => \common\models\business\BusinessBackpigFinish::$statues[$model->status]
                            ],
                        ],
                    ]) ?>
                </div>
            </div>

            <div class="col-md-10">
                <h4>结算的订单</h4>
                <?php foreach($model->backorder as $key => $row) { ?>
                    <?= Html::a($row['number'],\yii\helpers\Url::to(['/business/backpig/view','id'=>$row['id']]),['class'=>'btn btn-primary btn-xs','target'=>'_blank']) ?>
                <?php } ?>
            </div>
        </div>
    </section>


    <!--打印结算明细区域-->
    <div id="printarae" class="col-md-10 col-md-offset-1">

        <h4 style="margin-bottom: 10px;text-align: center">明细结算单</h4>

        <div class="col-md-8 col-md-offset-2">
            <span style="width: 50%;display: block;float: left;"><b>农户:<?= $print[0]['farmername'] ?></b></span>
            <span style="width: 125px;display: block;float: right;"><b>时间：<?= date('Y-m-d') ?></b></span>
        </div>
        <div class="col-md-8 col-md-offset-2">
            <?php
            foreach ($print as $key => $row) {
            $buytotal += $row['buycost'];
            $backtotal += ($row['backprice']*$row['grossweight']);
            ?>
            <table class="table table-bordered">
                <tr>
                    <th>仔猪编号</th>
                    <th>购买费用</th>
                    <th>回购费用</th>
                    <th>回购单审核人</th>
                </tr>

                <tr>
                    <td><?= $row['pigno'] ?></td>
                    <td><?= $row['buycost'] ?></td>
                    <td><?= $row['backprice']*$row['grossweight'] ?></td>
                    <td><?= $row['agreeuser'] ?></td>
                </tr>
                <table class="table table-bordered">
                    <tr>
                        <th>成本</th>
                    </tr>
                    <tr>
                        <td>
                            <table class="table" style="background-color: rgba(0, 0, 0, 0)">
                                <tr>
                                    <th>成本类型</th>
                                    <th>价格</th>
                                    <th>备注</th>
                                </tr>
                                <?php
                                $costdata = unserialize($row['cost']);
                                $total = 0;
                                foreach ($costdata as $k => $v) {
                                    $total += $v['price'];
                                    $costtotal += $v['price'];
                                    ?>
                                    <tr>
                                        <td><?= $v['type'] ?></td>
                                        <td><?= $v['price'] ?></td>
                                        <td><?= $v['description'] ?></td>
                                    </tr>
                                <?php } ?>
                                <tr>
                                    <td><b>总计</b></td>
                                    <td><?=$total?></td>
                                    <td><b>成本总计</b></td>
                                </tr>
                            </table>

                        </td>
                    </tr>
                </table>

                <?php } ?>

            </table>
        </div>
        <div class="col-md-8 col-md-offset-2">
            <div class="col-md-12">
                <div><b>仔猪总成本：<?= $buytotal+$backtotal-$costtotal ?></b></div>
                <div><div>仔猪购买费：<?=$buytotal?>；</div>
                    <div>仔猪养殖成本：<?=$costtotal?>；</div>
                    <div>理论仔猪回购费用：<?=$backtotal+$costtotal?>；<?php if(!$model->isNewRecord){ ?>实际决算回购费用：<?= $model->finishprice ?><?php } ?></div>
                </div>
            </div>
            <div class="col-md-12">
                <div><span>应付农户：<?= $backtotal+$costtotal ?></span><span>；实际付农户：<?= $model->finishprice ?></span></div>
                <div>结算人:<?= $model->finishuser ?></div>
                <div>结算日期:<?= date('Y-m-d',$model->createtime) ?></div>
            </div>

        </div>
        <div class="col-md-2 col-md-offset-10"><button class="btn btn-primary" id="print">打印</button></div>
    </div>
</section>


<script>
    <?php $this->beginBlock('js_end') ?>

    //打印
    $(function(){
        $("#print").printPreview({
            obj2print:'#printarae',
            width:'810'
        });

    });

    <?php $this->endBlock('jsend') ?>
    <?php $this->registerJs($this->blocks['js_end'],\yii\web\View::POS_END);//将编写的js代码注册到页面底部 ?>

</script>

